function[] = loocValidation(k, classNames)
% validates the results of the k-NN algorithm
%
%   INPUT
%   k...........an array of values k with witch the k-NN algorithm shall be
%               tested.
%   classNames..a cell array containing the class names for the validation

    % load the pictures with the given classes into variable 'pictures'
    pictures = cell(20 * size(classNames, 1), 1);
    % 'classes' will contain the class id for every picture in 'pictures'
    classes = zeros(1, 20 * size(classNames, 1));
    pos = 1;
    for i = 1 : size(classNames, 1)
        classPics = loadPictures(classNames{i});
        count = size(classPics, 1);
        newPos = pos + count - 1;
        [pictures{pos : newPos}] = deal(classPics{1 : count});
        classes(pos : newPos) = i;
        pos = newPos + 1;
    end
    % calculate the knowledge of the algorithm (store the feature vectors
    % and the class id in 'collectedData')
    collectedData = trainForKNN(pictures, classes);
    
    clError = zeros(1, size(k, 2));
    j = 1;
    for activeK = k
        errorPics = '';
        correct = 0;
        for i = 1 : size(classes, 2)
            % compare the k-NN-classification result with the ground truth
            if strcmp(kNNClassification(pictures{i}, activeK, collectedData, classNames, i), classNames{classes(i)})
                correct = correct + 1;
            else
                errorPics = [errorPics ' ' num2str(i)];
            end
        end
        clError(j) = (size(classes, 2) - correct) / size(classes, 2);
        disp(['k = ' num2str(activeK) '   classification error: ' num2str(clError(j)) ' (' num2str(correct) ' / ' num2str(size(classes, 2)) ') misclassified pictures (id):' errorPics]);
        j = j + 1;
    end
    plot(k, clError);
    title('Classification Error');
    xlabel('k');
    ylabel('error');
end
